package com.nanhu.test;

import java.io.InputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class AddressBookActivity extends ListActivity{
	
	Context mContext = null;
	
	private static final String[] PHONES_PROJECTION = new String[]{
		Phone.DISPLAY_NAME, Phone.NUMBER, Phone.PHOTO_ID, Phone.CONTACT_ID
	};
	
	/**联系人显示名称**/  
    private static final int PHONES_DISPLAY_NAME_INDEX = 0;
    
    /**电话号码**/  
    private static final int PHONES_NUMBER_INDEX = 1;
    
    /**头像ID**/  
    private static final int PHONES_PHOTO_ID_INDEX = 2;
    
    /**联系人的ID**/  
    private static final int PHONES_CONTACT_ID_INDEX = 3;
    
    /**联系人名称**/  
    private ArrayList<String> mContactsName = new ArrayList<String>();
    
    /**联系人头像**/  
    private ArrayList<String> mContactsNumber = new ArrayList<String>();
    
    /**联系人头像**/  
    private ArrayList<Bitmap> mContactsPhonto = new ArrayList<Bitmap>();
    
    ListView mListView = null;
    
    MyListAdapter myAdapter = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		mListView = this.getListView();
		
		getPhoneContacts();
		myAdapter = new MyListAdapter();
		setListAdapter(myAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mContactsNumber.get(position)));
				startActivity(dialIntent);
			}
			
		});
	}
	
	private void getPhoneContacts(){
		ContentResolver resolver = mContext.getContentResolver();
		Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null);
		if(phoneCursor != null){
			while(phoneCursor.moveToNext()){
				String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
				String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);
				Long contactId = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);
				Long photoId = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);
				
				Bitmap contactPhoto = null;
				if(photoId > 0){
					Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
					InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
					contactPhoto = BitmapFactory.decodeStream(input);
				}else{
					contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.a);
				}
				
				mContactsName.add(contactName);
				mContactsNumber.add(phoneNumber);
				mContactsPhonto.add(contactPhoto);
			}
			
			phoneCursor.close();
		}
	}
	
	private void getSIMContacts(){
		ContentResolver resolver = mContext.getContentResolver();
		Uri uri = Uri.parse("content://icc/adn");
		Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null, null);
		if(phoneCursor != null){
			while(phoneCursor.moveToNext()){
				String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
				if(TextUtils.isEmpty(phoneNumber))
					continue;
				String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);
				//Sim卡中没有联系人头像
				mContactsName.add(contactName);
				mContactsNumber.add(phoneNumber);
			}
			phoneCursor.close();
		}
	}
	
	class MyListAdapter extends BaseAdapter{
		
		@Override
		public int getCount() {
			return mContactsName.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView image = null;
			TextView title = null;
			TextView text = null;
			if(convertView == null || position < mContactsNumber.size()){
				convertView = LayoutInflater.from(mContext).inflate(R.layout.colorlist, null);
				image = (ImageView)convertView.findViewById(R.id.color_image);
				convertView.setTag(R.id.color_image, image);
				title = (TextView)convertView.findViewById(R.id.color_title);
				convertView.setTag(R.id.color_title, title);
				text = (TextView)convertView.findViewById(R.id.color_text);
				convertView.setTag(R.id.color_text, text);
			}else{
				image = (ImageView)convertView.getTag(R.id.color_image);
				title = (TextView)convertView.getTag(R.id.color_title);
				text = (TextView)convertView.getTag(R.id.color_text);
			}
			
			image.setImageBitmap(mContactsPhonto.get(position));
			title.setText(mContactsName.get(position));
			text.setText(mContactsNumber.get(position));
			return convertView;
		}
		
	}
}
